//index.js
//获取应用实例
const app = getApp()
const util = require("../../utils/util.js");

Page({
  data: {
    originPwd: "",
    newPwd: "",
    newConfirmPwd: "",
    disabled: true
  },
  //事件处理函数
  bindViewTap: function() {},
  // 页面加载
  onLoad: function() {
    console.log('index onload')
  },
  // 输入原来密码
  inputOrigin: function(event) {
    this.data.originPwd = event.detail.value;
    this.checkSaveBtnStatus();
  },
  // 输入新密码
  inputNew: function(event) {
    this.data.newPwd = event.detail.value;
    this.checkSaveBtnStatus();
  },
  // 输入确认新密码
  inputNewConfirm: function(event) {
    this.data.newConfirmPwd = event.detail.value;
    this.checkSaveBtnStatus();
  },
  // 检查确认按钮状态
  checkSaveBtnStatus: function() {
    // 如果有空
    if (this.data.originPwd == "" || this.data.newPwd == "" || this.data.newConfirmPwd == "") {
      this.setData({
        disabled: true,
      });
      return false;
    }
    if (this.data.newPwd.length !== this.data.newConfirmPwd.length){
      this.setData({
        disabled: true,
      });
      return false;
    }
    this.setData({
      disabled: false,
    });
    return true;
  },
  // 修改密码
  confirm: function() {
    if (this.data.newConfirmPwd !== this.data.newPwd) {
      util.toast("两次输入的新密码不一致");
      return false;
    }
    util.request("/api/account/modifyPwd", {
      password: this.data.originPwd,
      newPassword: this.data.newPwd,
    }, function(res){
      if (res.data.errCode != '0') {
        util.toast("原密码错误");
      } else {
        util.toast("修改成功")
        setTimeout(function(){
          wx.switchTab({
            url: '/pages/account/index',
          })
        }, 1000);
      }
    }, {"loadingHide": "imidate"});
  }
})